package fr.imag.ufrima.ctte.gui.views;

import java.awt.BorderLayout;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import fr.imag.ufrima.ctte.ImagCTTE;
import fr.imag.ufrima.ctte.Task;
import fr.imag.ufrima.ctte.TaskDiagram;
import fr.imag.ufrima.ctte.events.ImagCtteAdapter;
import fr.imag.ufrima.ctte.events.ImagCtteEvent;
import fr.imag.ufrima.ctte.events.TaskDiagramAdapter;
import fr.imag.ufrima.ctte.events.TaskDiagramEvent;
import fr.imag.ufrima.ctte.gui.editors.Editor;

public class PropertiesView extends View {		
	/**
	 * Constructor
	 */
	public PropertiesView(ImagCTTE imagCTTE){
		super(imagCTTE);
		InitComponents();
		imagCTTE.addImagCtteListener(new ImagCtteAdapter(){			
			public void taskDiagramSwitched(ImagCtteEvent event) {
				TaskDiagram taskDiagram = event.getTaskDiagram();
				if (taskDiagram != null){
					if (taskDiagram.getSelectedTask() != null){
						PropertiesView.this.removeAll();
						TaskPropertiesSubView tpsv = new TaskPropertiesSubView(getImagCTTE());
						PropertiesView.this.add(tpsv.getTitle(),tpsv);
					}else{
						PropertiesView.this.removeAll();
						DiagramPropertiesSubView dpsv = new DiagramPropertiesSubView(getImagCTTE());
						PropertiesView.this.add(dpsv.getTitle(),dpsv);
					}
					taskDiagram.addTaskDiagramListener(new TaskDiagramAdapter(){						
						public void selectedTaskChanged(TaskDiagramEvent event) {
							if (event.getTask() != null){
								PropertiesView.this.removeAll();
								TaskPropertiesSubView tpsv = new TaskPropertiesSubView(getImagCTTE());
								PropertiesView.this.add(tpsv.getTitle(),tpsv);
							}else{
								PropertiesView.this.removeAll();
								DiagramPropertiesSubView dpsv = new DiagramPropertiesSubView(getImagCTTE());
								PropertiesView.this.add(dpsv.getTitle(),dpsv);
							}
						}						
						public void selectedOperatorChanged(TaskDiagramEvent event) {
							if (event.getOperator() != null){
								PropertiesView.this.removeAll();
								OperatorPropertiesSubView opsv = new OperatorPropertiesSubView(getImagCTTE());							
								PropertiesView.this.add(opsv.getTitle(),opsv);
							}/*else{
								PropertiesView.this.removeAll();
								DiagramPropertiesSubView dpsv = new DiagramPropertiesSubView(getImagCTTE());
								PropertiesView.this.add(dpsv.getTitle(),dpsv);
							}*/
						}
					});
				}
			}
		});
	}
	/**
	 * Initialize components
	 */
	protected void InitComponents(){
		
		diagramPropertiesSubView = new DiagramPropertiesSubView(getImagCTTE());		
		this.add(diagramPropertiesSubView.getTitle(), diagramPropertiesSubView);
		/*this.addChangeListener(new ChangeListener(){
        	public void stateChanged(ChangeEvent event) {
        		if (imagCTTE != null){
        			if (PropertiesView.this.getComponentCount() > 0)
        				if (PropertiesView.this.getSelectedComponent()!=null)        					
        					imagCTTE.setCurrentTaskDiagram(((Editor)PropertiesView.this.getSelectedComponent()).getTaskDiagram());        				
        		}
        	}
        });*/
	}
	/**
	 * Components
	 */
	private DiagramPropertiesSubView diagramPropertiesSubView;
}
